package com.bobohost.crmrbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bobohost.crmrbac.dao.mapper.EmployeeRoleMapper;
import com.bobohost.crmrbac.dao.mapper.RoleMapper;
import com.bobohost.crmrbac.pojo.po.EmployeeRole;
import com.bobohost.crmrbac.pojo.po.QueryObject;
import com.bobohost.crmrbac.pojo.po.Role;
import com.bobohost.crmrbac.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 叶笑秋
 * @since 2025-05-21
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;

    @Override
    public IPage<Role> findRoleListPage(IPage page) {
        IPage<Role>  pageResult= baseMapper.selectPage(page, null);
        return pageResult;
    }

    @Override
    public PageInfo<Role> selectDepartmentByCondition(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<Role> list = roleMapper.selectByCondition(queryObject);
        return new PageInfo<>(list);
    }

    @Override
    public List<Role> findListByEmploeeId(Long id) {

        QueryWrapper qw = new QueryWrapper();
        qw.eq("employee_id",id);
        List<EmployeeRole> employeeRoleList = employeeRoleMapper.selectList(qw);
        if (null==employeeRoleList||employeeRoleList.isEmpty()){
            return null;
        }

        List<Role>roleList=new ArrayList<>();

        for (EmployeeRole employeeRole : employeeRoleList){
            Long roleId = employeeRole.getRoleId();
            Role role = roleMapper.selectById(roleId);
            roleList.add(role);
        }
        return roleList;
    }
}
